package com.chb.docter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chb.docter.dto.CommentDto;
import com.chb.docter.feigns.UserFeign;
import com.chb.docter.mapper.*;
import com.chb.docter.params.CommentParam;
import com.chb.docter.service.CommentService;
import com.hos.entity.his.*;
import com.hos.entity.sys.Comment;
import com.hos.entity.sys.User;
import com.hos.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: 陈海彬
 * @date: Created in 2023/12/25 9:13
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private UserFeign userFeign;
    @Resource
    private CareHistoryMapper careHistoryMapper;
    @Resource
    private PatientMapper patientMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private CareOrderMapper careOrderMapper;
    @Resource
    private CareOrderItemMapper careOrderItemMapper;
    @Resource
    private OrderChargeItemMapper orderChargeItemMapper;

    @Override
    public Comment addCommentByChId(CommentParam commentParam) {
        // 1.添加评论信息
        Integer commentId = this.insertComment(commentParam);
        // 2.修改病例单的评论id
        this.updateCareHistoryComment(commentId, commentParam.getChId());
        // 3.修改处方子项的状态
        this.updateCareOrderItemStatus(commentParam.getChId());
        return null;
    }

    /**
     * @param chId 病例id
     * @create by: 陈海彬
     * @description: 修改处方子项的状态
     * @create time: 2023/12/25 11:09
     */
    private void updateCareOrderItemStatus(String chId) {
        List<CareOrder> careOrderList = this.selectCareOrderList(chId);
        for (CareOrder careOrder : careOrderList) {
            LambdaQueryWrapper<CareOrderItem> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CareOrderItem::getCoId, careOrder.getCoId());
            List<CareOrderItem> orderItems = this.careOrderItemMapper.selectList(wrapper);
            for (CareOrderItem orderItem : orderItems) {
                orderItem.setStatus("3");
                this.careOrderItemMapper.updateById(orderItem);
                String itemId = orderItem.getItemId();
                // 2.修改支付信息
                LambdaUpdateWrapper<OrderChargeItem> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(OrderChargeItem::getItemId, orderItem.getItemId())
                        .set(OrderChargeItem::getStatus, "3");
                this.orderChargeItemMapper.update(updateWrapper);
            }
        }
    }


    /**
     * @param chId 病例id
     * @create by: 陈海彬
     * @description: 查询处方信息
     * @create time: 2023/12/25 11:09
     */
    private List<CareOrder> selectCareOrderList(String chId) {
        LambdaQueryWrapper<CareOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CareOrder::getChId, chId);
        return this.careOrderMapper.selectList(wrapper);
    }

    /**
     * @param commentId 评论表的id
     * @param chId      病例id
     * @create by: 陈海彬
     * @description: 修改病例单的评论id
     * @create time: 2023/12/25 11:05
     */
    private void updateCareHistoryComment(Integer commentId, String chId) {
        LambdaUpdateWrapper<CareHistory> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(CareHistory::getComment, commentId)
                .eq(CareHistory::getChId, chId);
        this.careHistoryMapper.update(wrapper);
    }


    /**
     * @param commentParam 评论参数
     * @create by: 陈海彬
     * @description: 添加评论信息
     * @create time: 2023/12/25 10:59
     */
    private Integer insertComment(CommentParam commentParam) {
        Comment comment = new Comment();
        User user = commentParam.getUser();
        comment.setValueId(user.getUserId());
        comment.setType(user.getUserId());
        // 设置评论内容
        comment.setContent(commentParam.getContent());
        // 设置患者id
        comment.setParentId(getPatientToken().getPatientId());
        // 设置评分等级
        comment.setStar(commentParam.getStar());
        comment.setAddTime(LocalDateTime.now());
        int insert = this.commentMapper.insert(comment);
        return comment.getId();
    }

    @Override
    public User getUserByChId(String chId) {
        Long userId = this.findUserByChId(chId);
        return userFeign.getUserById(userId);
    }

    @Override
    public Comment getAllByUserId(String id) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("value_id",id);
        return commentMapper.selectOne(wrapper);
    }

    @Override
    public List<CommentDto> getUserCommentById(String id) {
        return commentMapper.findComment(id);
    }

    private Long findUserByChId(String chId) {
        CareHistory careHistory = this.careHistoryMapper.selectById(chId);
        return careHistory.getUserId();
    }

    /**
     * @return com.hos.entity.sys.User
     * @create by: 陈海彬
     * @description: 获取请求用户的信息
     * @create time: 2023/12/12 10:39
     */
    private Patient getPatientToken() {
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getPhone, phone);
        Patient patient = this.patientMapper.selectOne(wrapper);
        patient.setPassword("");
        return patient;
    }
}